#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/Connector/lib -I/data/Software/mydan/Connector/private/lib
use strict;
use warnings;
use utf8;
use YAML::XS;
use Logs;
use LWP::UserAgent;

$| ++;

=head1 SYNOPSIS

    to => [ 'foo@open-c3.org', '123@open-c3.org' ],
    content => '',

=cut

my ( $usermesg, %env );
BEGIN{
    use Configini;
    $usermesg = Configini::get( 'usermesg' );
    %env = Configini::env( 'usermesgenv' );
};

return sub
{
    my %param = @_;

    my $ua = LWP::UserAgent->new;
    $ua->default_header( %env ) if %env;

    for my $to ( @{$param{to}} )
    {
        my $cont = JSON::to_json( +{ user => $to, mesg => $param{mesg} } );
        utf8::encode($cont);

        my $res = $ua->post(
            $usermesg,
            Content => $cont,
            'Content-Type' => 'application/json' 
        );

        if($res->is_success)
        {
            my $v = eval{decode_json $res->decoded_content};
            next if $v->{stat};
        }
        #TODO
        die "die" . YAML::XS::Dump $res;

    }
    return 1;
}
